<style lang="less" scoped>

</style>
<template>
    <div>
        <Row>
            <Col span="24">
                <Card style="margin-bottom: 10px">
                    <Form inline>
                        <FormItem style="margin-bottom: 0">
                            <Select v-model="searchConf.status" clearable placeholder='请选择状态' style="width:100px">
                                <Option :value="1">启用</Option>
                                <Option :value="0">禁用</Option>
                            </Select>
                        </FormItem>
                        <FormItem style="margin-bottom: 0">
                            <Select v-model="searchConf.type" clearable placeholder="请选择类别" style="width:100px">
                                <Option :value="1">用户账号</Option>
                                <Option :value="2">用户昵称</Option>
                            </Select>
                        </FormItem>
                        <FormItem style="margin-bottom: 0">
                            <Input v-model="searchConf.keywords" placeholder=""></Input>
                        </FormItem>
                        <FormItem style="margin-bottom: 0">
                            <Button type="primary" @click="search">搜索/刷新</Button>
                        </FormItem>
                    </Form>
                </Card>
            </Col>
        </Row>
        <Row>
            <Col span="24">
                <Card>
                    <p slot="title" style="height: 32px">
                        <Button type="primary" @click="alertAdd" icon="md-add">新增</Button>
                    </p>
                    <div>
                        <Table :loading="tabLoading" :columns="columnsList" :data="tableData" border>
                            <!--状态-->
                            <template slot-scope="{ row, index }" slot="status">
                                <i-switch v-model="row.status" :true-value="1" :false-value="0" @on-change="statusSwitch(row)" />
                            </template>
                            <!--操作-->
                            <template slot-scope="{ row, index }" slot="option">
                                <!--编辑-->
                                <Button type="primary" style="margin-right: 5px" @click="editShow(row,index)">编辑
                                </Button>
                                <!--删除-->
                                <Poptip word-wrap confirm transfer title="您确定要删除这条数据吗?" @on-ok="Del(row,index)">
                                    <Button type="error" :loading="row.loading">
                                        <span v-if="!row.loading">删除</span>
                                        <span v-else>删除</span>
                                    </Button>
                                </Poptip>
                            </template>
                        </Table>
                    </div>
                    <div class="margin-top-15" style="text-align: center">
                        <Page :total="tableShow.listCount" :current="tableShow.currentPage"
                              :page-size="tableShow.pageSize" @on-change="changePage"
                              @on-page-size-change="changeSize" show-elevator show-sizer show-total></Page>
                    </div>
                </Card>
            </Col>
        </Row>
        <Modal v-model="modalSetting.show" width="668" :styles="{top: '30px'}" @on-visible-change="doCancel">
            <p slot="header" style="color:#2d8cf0">
                <Icon type="md-alert"></Icon>
                <span>{{formItem.id ? '编辑' : '新增'}}用户</span>
            </p>
            <Form ref="myForm" :rules="ruleValidate" :model="formItem" :label-width="80">
                <FormItem label="用户账号" prop="username">
                    <Input v-model="formItem.username" placeholder="请输入账号"></Input>
                </FormItem>
                <FormItem label="用户昵称" prop="nickname">
                    <Input v-model="formItem.nickname" placeholder="请输入昵称"></Input>
                </FormItem>
                <FormItem label="用户密码" prop="password">
                    <Input v-model="formItem.password" type="password" placeholder="用户密码"></Input>
                </FormItem>
                <FormItem label="权限组" prop="group_id">
                    <CheckboxGroup v-model="formItem.group_id">
                        <Checkbox v-for="group in groupList" :key="group.id" :label="group.id + ''">{{ group.name }}
                        </Checkbox>
                    </CheckboxGroup>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancel" style="margin-right: 8px">取消</Button>
                <Button type="primary" @click="submit" :loading="modalSetting.loading">确定</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
    import {getUserIndex, changeStatus, add, edit, del} from '@/api/user'
    import {getGroups} from '@/api/auth'

    export default {
        name: 'system_user',
        data() {
            return {
                columnsList: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 65,
                        align: 'center'
                    },
                    {
                        title: '用户账号',
                        align: 'center',
                        key: 'username',
                        minWidth: 120
                    },
                    {
                        title: '用户昵称',
                        align: 'center',
                        key: 'nickname',
                        width: 160
                    },
                    {
                        title: '登录次数',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.userData === null ? '' : params.row.userData.login_times)
                        },
                        width: 100
                    },
                    {
                        title: '最后登录时间',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.userData === null ? '' : params.row.userData.last_login_time)
                        },
                        width: 170
                    },
                    {
                        title: '最后登录IP',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.userData === null ? '' : params.row.userData.last_login_ip)
                        },
                        width: 160
                    },
                    {
                        title: '状态',
                        align: 'center',
                        width: 100,
                        slot: 'status'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        width: 200,
                        slot: 'option'
                    }
                ],
                tableData: [],
                groupList: [],
                tableShow: {
                    currentPage: 1,
                    pageSize: 10,
                    listCount: 0
                },
                searchConf: {
                    type: '',
                    keywords: '',
                    status: ''
                },
                modalSetting: {
                    show: false,
                    loading: false,
                    index: 0
                },
                formItem: {
                    username: '',
                    nickname: '',
                    password: '',
                    group_id: [],
                    id: 0
                },
                ruleValidate: {
                    username: [
                        {required: true, message: '用户账号不能为空', trigger: 'blur'}
                    ],
                    nickname: [
                        {required: true, message: '用户昵称不能为空', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '用户密码不能为空', trigger: 'blur'}
                    ]
                },
                tabLoading: false // 加载表格数据
            }
        },
        created() {
            this.getList()
        },
        methods: {
            alertAdd() {
                let vm = this
                getGroups().then(response => {
                    vm.groupList = response.data.data.list
                })
                this.modalSetting.show = true
            },
            submit() {
                let vm = this
                this.$refs['myForm'].validate((valid) => {
                    if (valid) {
                        vm.modalSetting.loading = true
                        if (vm.formItem.id === 0) {
                            add(vm.formItem).then(response => {
                                vm.$Message.success(response.data.msg)
                                vm.getList()
                                vm.cancel()
                            }).catch(() => {
                                vm.modalSetting.loading = false
                            })
                        } else {
                            edit(vm.formItem).then(response => {
                                vm.$Message.success(response.data.msg)
                                vm.getList()
                                vm.cancel()
                            }).catch(() => {
                                vm.modalSetting.loading = false
                            })
                        }
                    }
                })
            },
            cancel() {
                this.modalSetting.show = false
            },
            doCancel(data) {
                if (!data) {
                    this.formItem.id = 0
                    this.$refs['myForm'].resetFields()
                    this.modalSetting.loading = false
                    this.modalSetting.index = 0
                }
            },
            changePage(page) {
                this.tableShow.currentPage = page
                this.getList()
            },
            changeSize(size) {
                this.tableShow.pageSize = size
                this.getList()
            },
            search() {
                this.tableShow.currentPage = 1
                this.getList()
            },
            getList() {
                let vm = this
                this.tabLoading = true
                let params = {
                    page: vm.tableShow.currentPage,
                    size: vm.tableShow.pageSize,
                    type: vm.searchConf.type,
                    keywords: vm.searchConf.keywords,
                    status: vm.searchConf.status
                }
                getUserIndex(params).then((response) => {
                    this.tabLoading = false
                    for (let v of response.data.data.list) {
                        v.loading = false
                    }
                    vm.tableData = response.data.data.list
                    vm.tableShow.listCount = response.data.data.count
                }).catch(() => {
                    this.tabLoading = false
                })
            },
            // 状态切换
            statusSwitch(row) {
                changeStatus(row.status, row.id).then(response => {
                    this.$Message.success(response.data.msg)
                    this.getList()
                })
            },
            // 显示编辑model
            editShow(row, index) {
                this.formItem.id = row.id
                this.formItem.username = row.username
                this.formItem.nickname = row.nickname
                this.formItem.password = 'ApiAdmin'
                this.formItem.group_id = row.group_id
                getGroups().then(response => {
                    this.groupList = response.data.data.list
                })
                this.modalSetting.show = true
                this.modalSetting.index = index
            },
            // 删除
            Del(row, index) {
                row.loading = true
                del(row.id).then(response => {
                    row.loading = false
                    this.tableData.splice(index, 1)
                    this.$Message.success(response.data.msg)
                }).catch(() => {
                    row.loading = false
                })
            }
        }
    }
</script>
